<?php

/**
 * User: rongxia
 * Date: 2018/09/10
 * Time: 10:04
 * Description:
 * 自动修复策略：
 *  定时重新跳到1个小时前去拉取数据，上两个小时都没有注单则不重新拉取
 */

namespace App\Console\Commands\ThirdOrder;

use App\Services\ThirdAPI\LEGAPIService;
use App\Services\ThirdAPI\GameApiService;
use App\Services\UserService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;
use Yii;

class LEGOrderProcessor extends BaseOrderProcessor {
    protected $signature = self::PREFIX . 'leg';
    protected $description = 'LEG棋牌注单拉取';

    public $legService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度(毫秒)，根据注单量调整
    public $timeInterval = (40 * 60 * 1000);
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;
    
    public $lockRedisKey = RedisKeys::LEG_LOCK_AUTOFIX;
    public $updateRedisKey = RedisKeys::LEG_UPDATE_TIME;

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->legService = new LEGAPIService();
        $this->partnerName = "LEG";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_CARD;
        $this->reportOrderType = self::ORDER_TYPE_CARD;
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        // 拉取 $this->timeInterval 的数据
        if (!is_numeric ($startTime)){
            $startTime = (strtotime ($startTime)) * 1000;
        }
        $this->latestUpdateKey = $endTime = min($this->getMillisecond (), $startTime + $this->timeInterval);
        $result = $this->legService->getOrders($startTime, $endTime);
        $this->latestUpdateKey = $result['end'];
        return $result['list'];
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        $this->updateTime = $data['update_time'];
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money']??0;
        $this->bonusMoney = $data['bonus_money']??0;
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }
    
    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey){
        Redis::connection()->set($this->updateRedisKey, $this->latestUpdateKey);
    }

    /*
     *获取微秒
     */
    public function getMillisecond() {
            list($t1, $t2) = explode(' ', microtime());
            return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }

}